// Project: eeprom2550
// FileName: main.c


#include "./USB/usb.h"
#include "./USB/usb_function_cdc.h"
#include "HardwareProfile.h"
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "usb_config.h"
#include "USB\usb_device.h"
#include "USB\usb.h"

#include <delays.h>
#include <adc.h>
#include <i2c.h>

#define lcd_DB LATB
#define lcd_E LATCbits.LATC7
#define lcd_RS LATCbits.LATC6

#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)


#pragma config FOSC=HSPLL_HS, PLLDIV=5, CPUDIV=OSC1_PLL2
#pragma config USBDIV=2, PWRT=ON, FCMEN=ON, IESO=ON
#pragma config BOR=ON, BORV=2, WDT=OFF, LVP=OFF, VREGEN=ON
#pragma config MCLRE=OFF, PBADEN=OFF, WDTPS=1024
#pragma config LPT1OSC=OFF, CCP2MX=OFF, DEBUG=OFF

#pragma config CP0=OFF, CP1=OFF, CP2=OFF, CP3=OFF, CPB=OFF
#pragma config CPD=OFF, WRT0=OFF, WRT1=OFF, WRT2=OFF, WRT3=OFF
#pragma config WRTB=OFF, WRTC=OFF, WRTD=OFF, EBTR0=OFF
#pragma config EBTR1=OFF, EBTR2=OFF, EBTR3=OFF, EBTRB=OFF


#pragma udata
char USB_In_Buffer[64];
char USB_Out_Buffer[64];

unsigned long NVV, NII, NW, NWW;
unsigned int N, M, vv, vi, adnl, adnh, adill, kk;
unsigned int j, jj, advvl, advvh, adiil, adiih;
unsigned int NV, NI;
unsigned char ADVH, ADVL, ADIH, ADIL, NH, NL;
unsigned char nl, nh, advl, advh, adil, adih, clr;
unsigned char i, sw, k;
unsigned char buf[35], bufn[4], bufv[5], bufi[5], bufw[10];


char V[] = "V";
char blank[] = " ";
char mA[] = "mA";
char Neq[] = "N=";


static void InitializeSystem(void);
void ProcessIO(void);
void USBDeviceTasks(void);
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
void UserInit(void);

void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);





#define REMAPPED_RESET_VECTOR_ADDRESS 0x00
#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x08
#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x18

#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
void Remapped_High_ISR (void){
_asm goto YourHighPriorityISRCode _endasm
}
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
void Remapped_Low_ISR (void){
_asm goto YourLowPriorityISRCode _endasm
}

#pragma code

#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode(){
USBDeviceTasks();
}
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode(){
}

#pragma code




void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay10TCYx(20);
lcd_E = 0;
}

void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}

void lcd_clr(void){
lcd_c4(0x01);
}

void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}

void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}

void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}


void lcd_putv(int v){
char i;
char buf[5];
v *=33;
for(i=0;i<5;i++)
buf[i]='0';
i=4;
do{
buf[i]=(v%10)+'0';
v=v/10;
i--;
} while(v>0);
lcd_putchr(buf[0]);
lcd_putchr('.');
for(i=1;i<4;i++)
lcd_putchr(buf[i]);
}


void lcd_puti(int v){
char i;
char buf[5];
v *=5;
for(i=0;i<5;i++)
buf[i]='0';
i=4;
do{
buf[i]=(v%10)+'0';
v=v/10;
i--;
} while(v>0);
lcd_putchr(buf[1]);
lcd_putchr(buf[2]);
lcd_putchr(buf[3]);
lcd_putchr('.');
lcd_putchr(buf[4]);
}


void lcd_putui(
unsigned int ui, unsigned char d){
unsigned char i;
unsigned char buf[5];
for(i=0; i<10; i++) buf[i] = ' ';
i = 4;
do{
buf[i] = (ui % 10) + '0';
ui = ui / 10;
i--;
} while(ui > 0);
for(i=(5-d); i<5; i++) lcd_putchr(buf[i]);
}


unsigned char EByteWrite( unsigned char control,
unsigned int address, unsigned char data ){

IdleI2C();
SSPCON2bits.SEN = 1;
while ( SSPCON2bits.SEN );
if ( PIR2bits.BCLIF ){
return ( -1 );
} else {
SSPBUF = control;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-2);
}
PIR1bits.SSPIF = 0;

SSPBUF = address >> 8;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-3);
}
PIR1bits.SSPIF = 0;

SSPBUF = address;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-4);
}
PIR1bits.SSPIF = 0;

SSPBUF = data;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-5);
}
PIR1bits.SSPIF = 0;

SSPCON2bits.PEN = 1;
while ( SSPCON2bits.PEN );
PIR1bits.SSPIF = 0;
if ( PIR2bits.BCLIF ){
return ( -1 );
}
return ( 0 );
}
}

unsigned int EByteRead( unsigned char control, unsigned int address ){

IdleI2C();
SSPCON2bits.SEN = 1;
while ( SSPCON2bits.SEN );
if ( PIR2bits.BCLIF ) {
return ( -1 );
} else {
SSPBUF = control;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-2);
}
PIR1bits.SSPIF = 0;

SSPBUF = address >> 8;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-3);
}
PIR1bits.SSPIF = 0;

SSPBUF = address;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-4);
}
PIR1bits.SSPIF = 0;

SSPCON2bits.RSEN = 1;
while ( SSPCON2bits.RSEN );
if ( PIR2bits.BCLIF ){
return ( -5 );
} else {
SSPBUF = control+1;
while(SSPSTATbits.BF);
IdleI2C();
if ( SSPCON2bits.ACKSTAT ){
return(-6);
}
PIR1bits.SSPIF = 0;

SSPCON2bits.RCEN = 1;
while ( SSPCON2bits.RCEN );
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while ( SSPCON2bits.ACKEN );
PIR1bits.SSPIF = 0;

SSPCON2bits.PEN = 1;
while ( SSPCON2bits.PEN );
if ( PIR2bits.BCLIF ){
return ( -7 );
} else {
PIR1bits.SSPIF = 0;
return ( (unsigned int) SSPBUF );
}
}
}
}


void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}

static void InitializeSystem(void){
USBDeviceInit();
}

void UserInit(void){

sw = 0; N = 0; j = 0;
}




void main(void){

InitializeSystem();


PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTE = 0x00;
TRISA = 0b00101011;
TRISB = 0b00000100;
TRISC = 0b00000000;

ADCON0 = 0b00000001;
ADCON1 = 0b00011011;
ADCON2 = 0b10110110;

OpenI2C(MASTER, SLEW_ON);
SSPADD = 9;

UserInit();
lcd_init();

if(USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE)){
USBDeviceAttach();
}
USBDeviceTasks();



while(1){

char i;
for(i=0; i<10; i++){

for(i=0; i<102; i++){
Delay_5mS;
}

SetChanADC(ADC_CH0);
ConvertADC();
while(BusyADC());
ADVH = ADRESH; ADVL = ADRESL;
vv = (ADVH*256) + ADVL;

SetChanADC(ADC_CH1);
ConvertADC();
while(BusyADC());
ADIH = ADRESH; ADIL = ADRESL;
vi = (ADIH*256) + ADIL;

lcd_clr();
lcd_locate(0, 1);
lcd_putv(vv);
lcd_puts(V);
lcd_puts(blank);
lcd_puti(vi);
lcd_puts(mA);

adnl = EByteRead(0xA0, 0);
adnh = EByteRead(0xA0, 1);
adill = EByteRead(0xA0, N*4);
M = adnh*256 + adnl;

lcd_locate(1, 0);
lcd_puts(Neq);
lcd_putui(M, 4);
lcd_puts(blank);
lcd_putui(adnl, 4);
lcd_putui(adill, 4);


if(PORTAbits.RA5 & PORTBbits.RB2){
if(N < 1000){
N++;
NH = N/256;
NL = N;

nl = EByteWrite(0xA0, 0, NL);
Delay10KTCYx(15);
nh = EByteWrite(0xA0, 1, NH);
Delay10KTCYx(15);
advl = EByteWrite(0xA0, N*4-2, ADVL);
Delay10KTCYx(15);
advh = EByteWrite(0xA0, N*4-1, ADVH);
Delay10KTCYx(15);
adil = EByteWrite(0xA0, N*4, ADIL);
Delay10KTCYx(15);
adih = EByteWrite(0xA0, N*4+1, ADIH);
Delay10KTCYx(15);
}
}


if(PORTEbits.RE3 == 0){
for(kk=0; kk<N*4+2; kk++){
clr = EByteWrite(0xA0, kk, 0);
Delay10KTCYx(1);
}
Delay10KTCYx(10);
N = 0;
}

}
ProcessIO();
}
}



void ProcessIO(void){

BYTE numBytesRead = getsUSBUSART(USB_Out_Buffer,64);
if(numBytesRead != 0){
BYTE i;

for(i=0; i<numBytesRead; i++){
switch(USB_Out_Buffer[i]){
case 0x0A: sw = 1;
default: break;
}
}
}

if(sw){
char m;
NW = 0;
for(j=1; j<M+1; j++){
for(m=0; m<2; m++){

if(mUSBUSARTIsTxTrfReady()){
advvl = EByteRead(0xA0, j*4-2);
advvh = EByteRead(0xA0, j*4-1);
adiil = EByteRead(0xA0, j*4);
adiih = EByteRead(0xA0, j*4+1);
NV = advvh*256 + advvl;
NV *= 33;
NI = adiih*256 + adiil;
NI *= 5;
NVV = NV;
NII = NI;
NW = NW + (NVV*NII)/10000;
NWW = NW;

for(i=0; i<8; i++) buf[i] = ' ';
for(i=8; i<13; i++) buf[i] = '0';
for(i=13; i<18; i++) buf[i] = ' ';
for(i=18; i<21; i++) buf[i] = '0';
for(i=21; i<27; i++) buf[i] = ' ';
for(i=27; i<32; i++) buf[i] = '0';
for(i=32; i<35; i++) buf[i] = ' ';

for(i=0; i<4; i++) bufn[i] = ' ';
for(i=0; i<5; i++) bufv[i] = ' ';
for(i=0; i<5; i++) bufi[i] = ' ';
for(i=0; i<10; i++) bufw[i] = ' ';

k = 3; jj = j;
do{
bufn[k] = (jj % 10) + '0';
jj = jj / 10;
k--;
} while(jj > 0);

k = 4;
do{
bufv[k] = (NV % 10) + '0';
NV = NV / 10;
k--;
} while(NV > 0);

k = 4;
do{
bufi[k] = (NI % 10) + '0';
NI = NI / 10;
k--;
} while(NI > 0);

k = 9;
do{
bufw[k] = (NWW % 10) + '0';
NWW = NWW / 10;
k--;
} while(NWW > 0);

buf[0]='\n'; buf[1]=bufn[0]; buf[2]=bufn[1];
buf[3]=bufn[2]; buf[4]=bufn[3];

buf[8]=bufv[0]; buf[9]='.'; buf[10]=bufv[1];
buf[11]=bufv[2]; buf[12]=bufv[3];

buf[16]=bufi[1]; buf[17]=bufi[2]; buf[18]=bufi[3];
buf[19]='.'; buf[20]=bufi[4];

buf[24]=bufw[3]; buf[25]=bufw[4]; buf[26]=bufw[5];
buf[27]=bufw[6]; buf[28]=bufw[7]; buf[29]=bufw[8];
buf[30]='.'; buf[31]=bufw[9];

for(i=0; i<35; i++){
USB_In_Buffer[i] = buf[i];
}
putUSBUSART(USB_In_Buffer, 35);
}
CDCTxService();
Delay_5mS;
}
}
}
sw = 0;
}

































// (ここから下は変更無し)



void USBCBSuspend(void)
{
#if defined(__C30__)
#if 0
U1EIR = 0xFFFF;
U1IR = 0xFFFF;
U1OTGIR = 0xFFFF;
IFS5bits.USB1IF = 0;
IEC5bits.USB1IE = 1;
U1OTGIEbits.ACTVIE = 1;
U1OTGIRbits.ACTVIF = 1;
Sleep();
#endif
#endif
}

#if 0
void __attribute__ ((interrupt)) _USB1Interrupt(void)
{
#if !defined(self_powered)
if(U1OTGIRbits.ACTVIF) {
IEC5bits.USB1IE = 0;
U1OTGIEbits.ACTVIE = 0;
IFS5bits.USB1IF = 0;

//USBClearInterruptFlag(USBActivityIFReg,USBActivityIFBitNum);
USBClearInterruptFlag(USBIdleIFReg,USBIdleIFBitNum);
//USBSuspendControl = 0;
}
#endif
}
#endif

void USBCBWakeFromSuspend(void)
{

}

void USBCB_SOF_Handler(void)
{

}

void USBCBErrorHandler(void)
{

}

void USBCBCheckOtherReq(void)
{
USBCheckCDCRequest();
}

void USBCBStdSetDscHandler(void)
{

}

void USBCBInitEP(void)
{
CDCInitEP();
}

void USBCBSendResume(void)
{
static WORD delay_count;

USBResumeControl = 1; // Start RESUME signaling

delay_count = 1800U; // Set RESUME line for 1-13 ms
do {
delay_count--;
} while(delay_count);
USBResumeControl = 0;
}

#if defined(ENABLE_EP0_DATA_RECEIVED_CALLBACK)
void USBCBEP0DataReceived(void)
{

}
#endif

BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size)
{
switch(event) {
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_BUS_ERROR:
USBCBErrorHandler();
break;
case EVENT_TRANSFER:
Nop();
break;
default:
break;
}
return TRUE;
}